package com.atguigu.flink.chapter07.state;

import com.atguigu.flink.bean.WaterSensor;
import com.atguigu.flink.chapter07.util.AtguiguUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.contrib.streaming.state.EmbeddedRocksDBStateBackend;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.io.IOException;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/8/14 9:49
 */
public class Flink08_State_Backend {
    public static void main(String[] args) throws IOException {
        // shift + ctrl + u
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
    
        env.enableCheckpointing(2000);
        
        // 设置状态后端
        //env.setStateBackend(new MemoryStateBackend());  //旧的写法 默认情况
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
        
        // fs
        //env.setStateBackend(new FsStateBackend("hdfs://hadoop162:8020/ck"));
    
        //env.setStateBackend(new HashMapStateBackend());
        //env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck");
        
        // rocksdb
//        env.setStateBackend(new RocksDBStateBackend("hdfs://hadoop162:8020/ck1"));
    
        env.setStateBackend(new EmbeddedRocksDBStateBackend());
        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop162:8020/ck1");
        
        
        
        env.setParallelism(2);
        // 每隔5s计算这5s内的wordCount
        env
            .socketTextStream("hadoop162", 9999)
            .map(new MapFunction<String, WaterSensor>() {
                @Override
                public WaterSensor map(String value) throws Exception {
                    String[] data = value.split(",");
                    return new WaterSensor(data[0], Long.valueOf(data[1]), Integer.valueOf(data[2]));
                }
            })
            .keyBy(WaterSensor::getId)
            .process(new KeyedProcessFunction<String, WaterSensor, String>() {
    
                private MapState<Integer, Object> vcMapState;
    
                @Override
                public void open(Configuration parameters) throws Exception {
        
                    vcMapState = getRuntimeContext().getMapState(
                        new MapStateDescriptor<Integer, Object>(
                            "vcMapState",
                            Integer.class,
                            Object.class
    
                        )
                    );
                }
                
                @Override
                public void processElement(WaterSensor input,
                                           Context ctx,
                                           Collector<String> out) throws Exception {
                    vcMapState.put(input.getVc(), new Object());
    
                    Iterable<Integer> keys = vcMapState.keys();
                    List<Integer> vcs = AtguiguUtil.toList(keys);
                    out.collect(ctx.getCurrentKey() + " 所有不重复的水位: " + vcs);
                    
                }
            })
            .print();
        
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
